define(function(require, exports, module){
	'use strict';
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var urlTool = require("resource/js/component/urlTool.js");
	var ajax = require("resource/js/component/ajax.js");
	
	var processKey;
	var dgutil = undefined;
	var datagridconfig = undefined;
	var config = {
		column:[[
	         {field:'nodeName',title:'节点名称',width:100,align:'center',height:50,
	        	 editor:{type:'textbox',options:{required:true,validType:'length[0,10]'}}},
	         {field:'markOfWait',title:'待审批标记',width:100,align:'center',height:50,
	        	 editor:{type:'textbox',options:{required:true,validType:'length[0,10]'}}},
	         {field:'markOfApproving',title:'审批中标记',width:100,align:'center',height:50,
	        	 editor:{type:'textbox',options:{required:true,validType:'length[0,10]'}}},
	         {field:'markOfApproved',title:'已通过标记',width:100,align:'center',height:50,
	        	 editor:{type:'textbox',options:{required:true,validType:'length[0,10]'}}},
	         {field:'isSeal',title:'印章',width:100,align:'center',height:50,formatter:function(value,row,index){
	        	 if(row.processNodeId !== 'PROCESS_START'){
	        		 var html = '';
	        		 if(row.isSeal == '1'){
	        			 html += '<input type="checkbox" role="isSeal" checked="checked" id="isSeal_'+index+'" class="chooseBtn" />';
	        		 }else{
	        			 html +='<input type="checkbox" role="isSeal" id="isSeal_'+index+'" class="chooseBtn" />';
	        		 }
		        	 return  html+'<label for="isSeal_'+index+'" class="choose-label"></label>'
		        	
	        	 }
	         }},
	         {field:'isSign',title:'签名',width:100,align:'center',height:50,formatter:function(value,row,index){
	        	 if(row.processNodeId !== 'PROCESS_START'){
	        		 var html = '';
	        		 if(row.isSign == '1'){
	        			 html += '<input role="isSign" type="checkbox" checked="checked" id="isSign_'+index+'" class="chooseBtn" />';
	        		 }else{
	        			 html +='<input role="isSign" type="checkbox" id="isSign_'+index+'" class="chooseBtn" />';
	        		 }
		        	 return  html+'<label for="isSign_'+index+'" class="choose-label"></label>'
		        	
	        	 }
	         }}
		]]
	};
	
	$('body').keyup(function(event){
	  if(event.keyCode ==13){
		  dgEditor.endEditing();
	  }
	});
	
	$('body').on('change','.chooseBtn',function(){
		var index =Number(this.id.split('_')[1]);
		$('#dg').datagrid('selectRow',index);
		var row = $('#dg').datagrid('getSelected');
		var flag = Number(this.checked).toString();
		var role = $(this).attr('role');
		var param = {
				nodeId:	row.node.processNodeId,
				key:row.node.processKey
		}

		if('isSeal' === role){
			param.isSeal = flag;
		}else if('isSign' === role){
			param.isSign = flag;
		}else{
			return
		}
		$('#dg').datagrid('loading');
		easyutil.ajaxHandler(
				BASEPATH+'/oa/act/userProcess/updateUserNodeSetting.htm',
				param,
				function(){
					$('#dg').datagrid('loaded');
					row[role] = flag;
					$('#dg').datagrid('updateRow',{
						index: index,
						row: row
					});
				});
		
	});
	
	var dgEditor = {
			editIndex:undefined,
			onClickRow:function(index){
				if (this.editIndex != index){
					if (this.endEditing()){
						$('#dg').datagrid('selectRow', index)
								.datagrid('beginEdit', index);
						this.editIndex = index;
					} else {
						$('#dg').datagrid('selectRow', this.editIndex);
					}
				}
			},
			endEditing:	function(){
				var editIndex = this.editIndex;
				if (editIndex == undefined){
						return true
					}
				if ($('#dg').datagrid('validateRow',editIndex)){
					var eds = $('#dg').datagrid('getEditors',editIndex);
					var row = $('#dg').datagrid('getRows')[editIndex];
					var updateObj = {key:processKey,nodeId:row.node.processNodeId}
					var isChange = false;
					$.each(eds,function(i,ed){
						var newValue = $(ed.target).textbox('getText');
						var fielId = ed.field;
						var oldValue = row[fielId];
						if(oldValue !== newValue){
							updateObj[fielId] = newValue;
							row[fielId] = newValue;
							isChange = true;
						}
					});
					$('#dg').datagrid('endEdit', editIndex);
					if(isChange){
						updateNode(updateObj);
					}
					return true;
				} else {
					return false;
				}
			}
	};
	
	function updateNode(param){
		ajax.init().success(function(code,msg,data){
			
		}).post(BASEPATH+'/oa/act/userProcess/updateUserNodeSetting.htm',param);
	}
	var initPage = function(){
		var buttons ={ 
				title:{
					_class:"",
					text:"节点配置"
				}
				};
			datagridconfig = {
					fitColumns:true,
					singleSelect:true,
					striped:true,
					autoRowHeight:false,
					url:BASEPATH+'/oa/act/userProcess/queryUserNodesSettingByPage.htm',
					toolbar:buttons,
					columns:config.column,
					eid:'dg',
					args:'searchform',
					onClickRow:function(rowIndex, rowData){
						dgEditor.onClickRow(rowIndex);
					},
			};
			using(["datagrid","parser","textbox","messager"],function(){
				//初始化主表datagrid
				processKey = urlTool.getParam("key");
				$("#key").val(processKey);
				dgutil = new easyutil.ajaxDataGrid(datagridconfig);
			});
	};
	module.exports = {
		init:initPage
	}
});